<?php
namespace app\home\controller;

use think\Controller;
use PHPMailer\PHPMailer\PHPMailer;

class Common extends Controller{

    //随机数
    public function get_random2($length = 6){
        $min = pow(10, ($length - 1));
        $max = pow(10, $length) - 1;
        return mt_rand($min, $max);
    }

    public function phpMail($email,$subject,$body,$debug = 0){
        $mail = new PHPMailer ();
        try{
            //邮件调试模式
            $mail->SMTPDebug = $debug;
            //设置邮件使用SMTP
            $mail->isSMTP();
            // 设置邮件程序以使用SMTP
            $mail->Host = 'smtp.qq.com';
            // 设置邮件内容的编码
            $mail->CharSet='UTF-8';
            // 启用SMTP验证
            $mail->SMTPAuth = true;
            // SMTP username
            $mail->Username = '13666024914@qq.com';
            // SMTP password
            $mail->Password = 'cgzlbauxrtmqbjjf';
            // 启用TLS加密，`ssl`也被接受
//            $mail->SMTPSecure = 'tls';
            // 连接的TCP端口
            $mail->Port = 587;
            //设置发件人
            $mail->setFrom('328224536@qq.com', '海宽达');
            //  添加收件人1
            $mail->addAddress($email,$email);     // Add a recipient
//            $mail->addAddress('ellen@example.com');               // Name is optional
//            收件人回复的邮箱
            $mail->addReplyTo('328224536@qq.com', '海宽达');
//            抄送
//            $mail->addCC('cc@example.com');
//            $mail->addBCC('bcc@example.com');
            //附件
//            $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//            $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
            //Content
            // 将电子邮件格式设置为HTML
            $mail->isHTML(true);
            $mail->Subject = $subject;
            $mail->Body    = $body;
//            $mail->AltBody = '这是非HTML邮件客户端的纯文本';
            $mail->send();
            return true;
        }catch (Exception $e){
            return 'Mailer Error: ' . $mail->ErrorInfo;
        }
    }

}
